/*
*  Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
*/
package com.pitop.material.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.MaterialCurrencyException;
import com.pitop.material.api.MaterialCurrencyFeignApi;
import com.pitop.material.entity.MaterialCurrency;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 币种
 *
 * @author berton
 */
@Service("materialCurrencySVImpl")
@Slf4j
public class MaterialCurrencySVImpl {

    @Autowired
    private MaterialCurrencyFeignApi materialCurrencyFeignApi;

    /**
     * 加载一个对象MaterialCurrency
     *
     * @param id * @param code 编码
     * @return MaterialCurrency
     */
    public MaterialCurrency load(Long id, String code) {
        if (id == null && code == null) {
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialCurrencyFeignApi.load(id, code);
    }

    /**
     * 加载一个对象MaterialCurrency 通过id
     *
     * @param id
     * @return MaterialCurrency
     */
    public MaterialCurrency loadById(Long id) {
        if (id == null) {
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialCurrencyFeignApi.loadById(id);
    }

    /**
     * 加载一个对象MaterialCurrency 通过code
     *
     * @param code 编码
     * @return MaterialCurrency
     */
    public MaterialCurrency loadByCode(String code) {
        if (code == null) {
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialCurrencyFeignApi.loadByCode(code);
    }


    /**
     * 删除对象MaterialCurrency
     *
     * @param id * @param code 编码
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        materialCurrencyFeignApi.delete(id, code);
    }


    /**
     * 查询MaterialCurrency分页
     *
     * @param materialCurrency 对象
     * @param curPage          当前页
     * @param pageSize         分页条数
     * @return List<MaterialCurrency>
     */
    public List<MaterialCurrency> list(MaterialCurrency materialCurrency, int curPage, int pageSize) {
        return materialCurrencyFeignApi.list(materialCurrency, curPage, pageSize);
    }

    /**
     * 查询MaterialCurrency分页
     *
     * @param id       * @param code  编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<MaterialCurrency>
     */
    public List<MaterialCurrency> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        MaterialCurrency materialCurrency = new MaterialCurrency();
        materialCurrency.setId(id);
        materialCurrency.setCode(code);

        return materialCurrencyFeignApi.list(materialCurrency, curPage, pageSize);
    }


    public int count(Long id, String code) {
        return materialCurrencyFeignApi.count(id, code);
    }


    public int count(MaterialCurrency materialCurrency) {
        return materialCurrencyFeignApi.count(materialCurrency);
    }


    /**
     * 保存
     *
     * @param materialCurrency 实体
     * @throws BaseException
     */
    public void save(MaterialCurrency materialCurrency) {
        if (materialCurrency == null) {
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //判断币种名称是否已经存在
        MaterialCurrency materialCurrencyFlag = materialCurrencyFeignApi.loadByCurrency(materialCurrency.getCurrency());
        if (materialCurrencyFlag != null) {
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Exists);
        }
        materialCurrencyFeignApi.build(materialCurrency);
    }

    /**
     * 更新
     *
     * @param materialCurrency 实体
     * @throws BaseException
     */
    public void modify(MaterialCurrency materialCurrency) {
        if (materialCurrency == null) {
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //判断币种名称是否已经存在
        MaterialCurrency materialCurrencyFlag = materialCurrencyFeignApi.loadByCurrency(materialCurrency.getCurrency());
        if (materialCurrencyFlag != null && !materialCurrency.getCurrency().equals(materialCurrencyFlag.getCode())) {
            throw new MaterialCurrencyException(BaseException.BaseExceptionEnum.Exists);
        }
        materialCurrencyFeignApi.modify(materialCurrency);
    }

}
